﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Linq;

namespace ComLib.ConfigService
{
    public static class XZQHService
    {
        public static string BaseDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        private static Dictionary<string, string> _citys = new Dictionary<string, string>();

        private static bool _bInited = false;

        public static bool Inited
        {
            get { return _bInited; }
        }

        public static void Init()
        {
            _citys.Clear();
            string xmlFile = string.Format("{0}\\config\\citys.xml", BaseDirectory);
            XDocument xmlDoc = XDocument.Load(xmlFile);
            XElement root = xmlDoc.Element("root");
            foreach(XElement province in root.Elements("Level1"))
            {
                string provinceName = province.Attribute("key").Value;
                string Value = province.Attribute("value").Value;
                if (!_citys.ContainsKey(Value))
                {
                    _citys.Add(Value, provinceName);
                }
                foreach (XElement shi in province.Elements("Level2"))
                {
                    string shiName = provinceName + shi.Attribute("key").Value;
                    Value = shi.Attribute("value").Value;
                    if (!_citys.ContainsKey(Value))
                    {
                        _citys.Add(Value, shiName);
                    }
                    foreach (XElement xian in shi.Elements("Level3"))
                    {
                        string xianName = shiName + xian.Attribute("key").Value;
                        Value = xian.Attribute("value").Value;
                        if(!_citys.ContainsKey(Value))
                        {
                            _citys.Add(Value, xianName);
                        }                      
                    }
                }
            }
            _bInited = true;
        }

        public static string GetDZName(string code)
        {
            if(!_bInited)
            {
                return "";
            }
            if (_citys.ContainsKey(code))
            {
                return _citys[code];
            }
            return "";
        }
    }
}
